package cn.glutnn.service.impl;

import cn.glutnn.entity.FinancingIntention;
import cn.glutnn.entity.Result;
import cn.glutnn.entity.dto.RecommendDTO;
import cn.glutnn.mapper.FinanceMapper;
import cn.glutnn.service.FinanceService;
import cn.glutnn.service.FinancingIntentionService;
import cn.glutnn.utils.ThreadLocalUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.glutnn.entity.Finance;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * (Finance)表服务实现类
 *
 * @author msh
 * @since 2024-10-25 09:33:55
 */
@Service("financeService")
public class FinanceServiceImpl extends ServiceImpl<FinanceMapper, Finance> implements FinanceService {

    @Autowired
    private FinancingIntentionService financingIntentionService;

    @Autowired
    private FinanceMapper financeMapper;


    @Override
    public Result<List<RecommendDTO>> selectRecommendByName() {
        Map o = ThreadLocalUtil.get();
        String username = (String) o.get("username");
        FinancingIntention financingIntention = financingIntentionService.selectIntentionByName(username.toString());
        if (StringUtils.isEmpty(financingIntention)) {
            List<RecommendDTO> recommendDTOS = financeMapper.selectWithNoneIntention(username.toString());
            return Result.success(recommendDTOS);
        } else {
            List<RecommendDTO> recommendDTOS = financeMapper.selectAllRecommend(username);
            return Result.success(recommendDTOS);
        }
    }

    @Override
    public Result<Finance> add(Finance finance) {
        Map o = ThreadLocalUtil.get();
        Object o1 = o.get("username");
        finance.setOwnName(o1.toString());
        finance.setStatus(0);
        finance.setCreateTime(new Date());
        finance.setUpdateTime(new Date());
        financeMapper.insertSelective(finance);

        return Result.success();
    }

    @Override
    public Result<Finance> addFinanceMulti(Finance finance) {
        Map o = ThreadLocalUtil.get();
        Object o1 = o.get("username");
        finance.setOwnName(o1.toString());
        finance.setStatus(0);
        finance.setCreateTime(new Date());
        finance.setUpdateTime(new Date());

        boolean save = save(finance);

        return Result.success();
    }

    @Override
    public Result<List<Finance>> selectByName() {
        Map o = ThreadLocalUtil.get();
        Object o1 = o.get("username");
        List<Finance> list = list(new QueryWrapper<Finance>().eq("own_name", o1.toString()));

        return Result.success(list);
    }

    @Override
    public Result<Finance> updateFinanceById(Finance finance) {

        boolean b = updateById(finance);

        return Result.success();
    }

    @Override
    public Result<Finance> deleteById(Integer id) {
        boolean remove = remove(new QueryWrapper<Finance>().eq("finance_id", id));


        return Result.success();
    }

    @Override
    public Result<Finance> selectById(Integer id) {
        Finance finance = getById(id);

        return Result.success(finance);
    }


}

